/*
 * $Id: FullscreenWindow.java 157 2009-05-22 11:36:08Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.swing.display;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * FullscreenWindow.
 * 
 * Main window using Java fullscreen exclusive mode.
 * 
 * @author Andreas Meyer
 * @version $Revision: 157 $
 */

public class FullscreenWindow {

	/** The device for fullscreen. */
	private GraphicsDevice device;

	/** The original display mode. */
	private DisplayMode originalDisplayMode;
	
	/** The Swing window. */
	private final JFrame window;

	/** The flag for fullscreen mode. */
	private boolean showFullscreen = false;


	/**
	 * Construct the window.
	 */
	public FullscreenWindow() {
		this(true);
	}

	/**
	 * Construct the window.
	 * 
	 * @param showFullscreen
	 */
	public FullscreenWindow(final boolean showFullscreen) {
		this.showFullscreen = showFullscreen;
		
		window = new JFrame();
		window.setBackground(Color.BLACK);
		window.getContentPane().setBackground(Color.BLACK);
		window.setResizable(false);
	}
	
	/**
	 * Switch to fullscreen.
	 * 
	 * @param displayNumber
	 * @param name
	 */
	public void show(final int displayNumber,
			         final String name,
			         final JComponent mainComponent) {
		
		// resolve screen devices
		final GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		final GraphicsDevice[] devices = ge.getScreenDevices();
		if (displayNumber >= devices.length) {
			
			throw new IllegalArgumentException(
					"Display " + displayNumber + " not found");
		} // end if

		// store display stuff
		device = devices[displayNumber];
		originalDisplayMode = device.getDisplayMode();

		window.setName(name);
		window.setTitle(name);

		// switch to fullscreen mode
		if(showFullscreen) {
			window.setUndecorated(true);
			device.setFullScreenWindow(window);
		} else {
			window.setSize(640, 480);
			window.setVisible(true);
		} // end if
		
		window.getContentPane().add(mainComponent);
		window.validate();
	}
	
	/**
	 * Switch to original mode.
	 * 
	 */
	public void hide() {
		
		// switch back to original mode
		if(showFullscreen) {
			device.setDisplayMode(originalDisplayMode);
		}
		window.dispose();
	}
	
	/**
	 * Return the window width.
	 * 
	 * @return
	 */
	public int getWidth() {
		return window.getWidth();
	}

	/**
	 * Return the window height.
	 * 
	 * @return
	 */
	public int getHeight() {
		return window.getHeight();
	}
	
	/**
	 * Add a key listener.
	 * 
	 * @param l
	 */
	public void addKeyListener(final KeyListener l) {
		window.addKeyListener(l);
	}
	
	/**
	 * Toggle fullscreen mode.
	 */
	public void toggleFullscreen() {
		//TODO: implement
	}

}
